package com.jdpc.weparty.component

import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.system.SystemProperties
import org.springframework.boot.web.context.WebServerInitializedEvent
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.UnknownHostException


@Component
class ServerConfig : ApplicationListener<WebServerInitializedEvent> {
    private var serverPort = 0;

    @Value("\${users.avatar.relative}")
    lateinit var avatarPath: String

    @Value("\${activities.banner.relative}")
    lateinit var bannerPath: String

    @Value("\${jwt.header}")
    lateinit var tokenHeader: String

    @Value("\${jwt.tokenHead}")
    lateinit var tokenHead: String


    @Value("\${jwt.secret}")
    lateinit var secret: String

    @Value("\${jwt.expiration}")
    var expiration: Long? = null

    var localPath = SystemProperties.get("user.dir")

//    fun getUrl(): String {
//        val address = pickHost(getAllIpAddress())
//        return "http://" + address + ":" + this.serverPort
//    }
//
//    fun pickHost(list: List<Pair<NetworkInterface, InetAddress>>): String? {
//        list.forEach {
//            if (it.first.name.contains("wlan")) {
//                return it.second.hostAddress
//            }
//        }
//        return null
//    }
//
//    fun getAllIpAddress(): List<Pair<NetworkInterface, InetAddress>> {
//        //get all network interface
//        val allNetworkInterfaces = NetworkInterface.getNetworkInterfaces()
//        var networkInterface: NetworkInterface? = null
//
//        val list = ArrayList<Pair<NetworkInterface, InetAddress>>()
//        //check if there are more than one network interface
//        while (allNetworkInterfaces.hasMoreElements()) {
//            //get next network interface
//            networkInterface = allNetworkInterfaces.nextElement()
//            //output interface's name
//            println("network interface: " +
//                    networkInterface.displayName + ";" + networkInterface.name)
//
//            //get all ip address that bound to this network interface
//            val allInetAddress = networkInterface.inetAddresses
//            var ipAddress: InetAddress? = null
//
//            //check if there are more than one ip addresses
//            //band to one network interface
//            while (allInetAddress.hasMoreElements()) {
//                //get next ip address
//                ipAddress = allInetAddress.nextElement()
//                if (ipAddress != null && ipAddress is Inet4Address) {
//                    println("ip address: " +
//                            ipAddress.getHostAddress())
//                    list.add(Pair(networkInterface, ipAddress))
//                }
//            }
//        }
//        return list
//    } //end method getAllIpAddress

    override fun onApplicationEvent(event: WebServerInitializedEvent) {
        this.serverPort = event.webServer.port
    }
}

